package com.yazi.mxz.person.controller.firm;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yazi.mxz.common.entity.custom.GoodsReportView;
import com.yazi.mxz.common.service.GoodsReportViewService;

@Controller("firm-indexController")
@RequestMapping("/firm")
public class CustomIndexController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(CustomIndexController.class);
	
	@Resource
	GoodsReportViewService goodsReportViewService;
	
	@RequestMapping
	public String index(Model model) {
		debug(log, "进入企业管理首页");
		
		// 今天
		GregorianCalendar cal = new GregorianCalendar();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		long startTime = cal.getTimeInMillis();
		cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		long endTime = cal.getTimeInMillis();
		
		// 今日销售额
		List<GoodsReportView> list = goodsReportViewService.sumByCompanyId(getCompanyId(), startTime, endTime);
		Float revenue = null;
		if (null != list && list.size() > 0) {
			revenue = list.get(0).getRevenue();
		}
		if (null == revenue) {
			revenue = 0f;
		}
		
		model.addAttribute("revenue", revenue);
		model.addAttribute("company", getCompany());
		return "firm/index";
	}
}
